package com.cljz.makepassenger.base;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;

import com.cljz.makepassenger.utils.T;
import com.cljz.makepassenger.widget.LoadingDialog;

import java.lang.ref.WeakReference;

import cn.droidlover.xdroidmvp.mvp.IPresent;
import cn.droidlover.xdroidmvp.mvp.XLazyFragment;

/**
 * Created by apple on 2017/4/26.
 */

public abstract class BaseLazyFragment<P extends IPresent> extends XLazyFragment<P> {

    private static final int DISMISS_DIALOG = 233;
    private LoadingDialog loadingDialog;
    final Handler dialogHandler = new DialogHandler(this);
    public static final String ARGUMENT = "argument";
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState) {
        super.onCreateViewLazy(savedInstanceState);
        initListener();

    }

    protected void initListener() {

    }

    private static class DialogHandler extends Handler {
        private final WeakReference<BaseLazyFragment> mFragment;

        DialogHandler(BaseLazyFragment fragment) {
            mFragment = new WeakReference<>(fragment);
        }

        @Override
        public void handleMessage(Message msg) {
            BaseLazyFragment fragment = mFragment.get();
            switch (msg.what) {
                case DISMISS_DIALOG:
                    fragment.dismissLoadingDialog();
                    T.getInstance().showShort("网络超时");
                    break;
            }
        }
    }

    public void showLoadingDialog() {
        loadingDialog = new LoadingDialog(App.getInstance());
        loadingDialog.show();
        dialogHandler.sendEmptyMessageDelayed(DISMISS_DIALOG, 10000);
    }

    public void dismissLoadingDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }
}
